package com.cos.emp.web.controller;

import com.cos.emp.entity.Emp;
import com.cos.emp.services.EmpServices;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author: COS
 * @time: 2022/9/30 14:35
 * @description:
 */
@Controller
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private EmpServices empServices;

    @RequestMapping("/updateEmp")
    public String updateEmp(Emp emp,Model model){
        if(empServices.updateEmp(emp)){
            model.addAttribute("msg", "修改成功");
        }else {
            model.addAttribute("msg", "修改失败");
        }
        return "message";
    }
    @RequestMapping("/getEmpByEno")
    public String getEmpByEno(Integer eno,Model model){
        model.addAttribute("e", empServices.getEmpByEno(eno));
        return "updateEmp";
    }
    @RequestMapping("/addEmp")
    public String addEmp(Emp emp,Model model){
        if (empServices.addEmp(emp)) {
            model.addAttribute("msg", "新增成功");
        }else{
            model.addAttribute("msg", "新增失败");
        }
        return "message";
    }

    @RequestMapping("/checkName")
    @ResponseBody
    public String checkEmpName(String ename) {
        if (empServices.checkName(ename)) {
            // 返回音响 用户名不可用
            return "0";
        }
        // 返回响应 用户名可用
        return "1";
    }

    @RequestMapping("/del")
    @ResponseBody
    public String delEmp(Integer eno) {
        if (empServices.delEmp(eno)) {
            return "1";
        }
        return "0";
    }

    @RequestMapping("/getEmpByPage")
    public String getEmpByPage(@RequestParam Map<String, String> emps, Model model) {
        PageInfo<Emp> info = empServices.getEmpByPage(emps);
        model.addAttribute("info", info);
        return "index";
    }


    @RequestMapping("/getAll")
    @ResponseBody
    public List<Emp> getAll() {
        return empServices.getAll();
    }
}
